package com.xiaokong.changerecord;


import com.xiaokong.changerecord.aop.ChangeRecordAspect;
import com.xiaokong.changerecord.impl.RedisChangeRecordImpl;
import com.xiaokong.changerecord.impl.RedisDictImpl;
import com.xiaokong.changerecord.properties.ChangeRecordProperties;
import com.xiaokong.changerecord.strategy.ChangeRecordStrategy;
import com.xiaokong.changerecord.strategy.DictStrategy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * 变更记录配置类
 *
 * @author Hu_bx
 * @date 17:17 2023/9/15
 **/
@Configuration
@EnableConfigurationProperties(ChangeRecordProperties.class)
public class ChangeRecordAutoConfiguration {

    /**
     * 默认使用内存字典
     *
     * @return com.jingdianjichi.changerecord.strategy.DictStrategy
     * @author Hu_bx
     * @date 14:59 2023/9/16
     **/
    @Bean
    @ConditionalOnMissingBean
    public DictStrategy dictStrategy(StringRedisTemplate redisTemplate) {
        return new RedisDictImpl(redisTemplate);
    }

    /**
     * 默认使用内存存储
     *
     * @return com.jingdianjichi.changerecord.strategy.DictStrategy
     * @author Hu_bx
     * @date 14:59 2023/9/16
     **/
    @Bean
    @ConditionalOnBean(DictStrategy.class)
    @ConditionalOnMissingBean
    public ChangeRecordStrategy changeRecordStrategy(DictStrategy dictStrategy, StringRedisTemplate redisTemplate, ChangeRecordProperties changeRecordProperties) {
        return new RedisChangeRecordImpl(redisTemplate, dictStrategy, changeRecordProperties);
    }

    /**
     * 变更记录AOP
     *
     * @param changeRecordStrategy changeRecordStrategy
     * @return com.jingdianjichi.changerecord.aop.ChangeRecordAspect
     * @author Hu_bx
     * @date 14:59 2023/9/16
     **/
    @Bean
    @ConditionalOnMissingBean
    public ChangeRecordAspect changeRecordAspect(ChangeRecordStrategy changeRecordStrategy) {
        return new ChangeRecordAspect(changeRecordStrategy);
    }
}
